package com.sk.weichat.ui.me.wallet;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.sk.weichat.R;
import com.sk.weichat.bean.redpacket.RedPacketNoticeBean;
import com.sk.weichat.bean.redpacket.RedPacketNoticeInfoBean;
import com.sk.weichat.db.dao.RedPacketNoticeDao;
import com.sk.weichat.ui.base.BaseActivity;
import com.sk.weichat.ui.tool.WebViewActivity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author lzs
 * @date 2019/10/10
 * Email:343067508@qq.com
 **/
public class WalletNotificationsActivity extends BaseActivity {
    BaseQuickAdapter<WalletNoticeBean, BaseViewHolder> adapter;
    List<WalletNoticeBean> data = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wallet_notifications);
        findViewById(R.id.iv_title_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        TextView tvRight = findViewById(R.id.tv_title_right);
        tvRight.setText("交易记录");
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转交易记录
                String url = coreManager.getConfig().TRADE_RECORDE + "?access_token=" + coreManager.getSelfStatus().accessToken + "&time=" + System.currentTimeMillis() / 1000;
                WebViewActivity.start(mContext, url, false);
            }
        });
        TextView tvTitle = findViewById(R.id.tv_title_center);

        tvTitle.setText(getResources().getString(R.string.title_wallet_notification));
        RecyclerView recyclerview = findViewById(R.id.recyclerview);
        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));

        adapter = new BaseQuickAdapter<WalletNoticeBean, BaseViewHolder>(R.layout.item_wallet_notice, data) {
            //消息时间
            SimpleDateFormat sdf1 = new SimpleDateFormat("MM月dd日 HH:mm");
            //消息时间
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");

            @Override
            protected void convert(BaseViewHolder holder, WalletNoticeBean item) {
                holder.setText(R.id.tv_title, item.title);
                holder.setText(R.id.tv_time, sdf1.format(new Date(item.time * 1000)));
                holder.setText(R.id.tv_refund_time, sdf2.format(new Date(item.refundTime * 1000)));
                holder.setText(R.id.tv_money, item.money + "");
                holder.setText(R.id.tv_type, item.refundType);
                if (!TextUtils.isEmpty(item.source) && item.source.length() > 12) {
                    item.source = item.source.substring(0, 12) + "...";
                }
                holder.setText(R.id.tv_source, item.source);
                holder.setText(R.id.tv_reason, item.reason);
            }
        };
        recyclerview.setAdapter(adapter);
        //加载数据
        loadData();
        //排序
        Collections.sort(data);
        //刷新
        adapter.notifyDataSetChanged();
        if (data.size() == 0) {
            findViewById(R.id.iv_empty).setVisibility(View.VISIBLE);
            findViewById(R.id.tv_empty).setVisibility(View.VISIBLE);
        } else {
            findViewById(R.id.iv_empty).setVisibility(View.GONE);
            findViewById(R.id.tv_empty).setVisibility(View.GONE);
        }
    }

    /**
     * 设置为已读
     */
    private void readUpdate(RedPacketNoticeBean bean) {
        bean.setIsRead(RedPacketNoticeBean.read);
        RedPacketNoticeDao.getInstance().update(bean);
    }


    private void loadData() {
        List<RedPacketNoticeBean> redpacketNoticeList = RedPacketNoticeDao.getInstance().queryAll();
        //
        List<WalletNoticeBean> walletNoticeBeans = redPacketNoticeBean2WalletNoticeBean(redpacketNoticeList);
        data.addAll(walletNoticeBeans);
    }

    Gson gson = new Gson();

    private List<WalletNoticeBean> redPacketNoticeBean2WalletNoticeBean(List<RedPacketNoticeBean> redpacketNoticeList) {
        List<WalletNoticeBean> list = new ArrayList<>();
        for (int i = 0; i < redpacketNoticeList.size(); i++) {
            //更新已读状态
            readUpdate(redpacketNoticeList.get(i));
            RedPacketNoticeInfoBean infoBean = null;
            //Log.e("lzs", "RedPacketNoticeBean:" + redpacketNoticeList.get(i));
            try {
                //Log.e("lzs", "getObjectId:" + redpacketNoticeList.get(i).getObjectId());
                infoBean = gson.fromJson(redpacketNoticeList.get(i).getObjectId(), RedPacketNoticeInfoBean.class);
            } catch (Exception e) {

            }
            if (infoBean != null) {
                WalletNoticeBean walletNoticeBean = new WalletNoticeBean();
                walletNoticeBean.money = infoBean.getMoney();
                walletNoticeBean.reason = infoBean.getReason();
                walletNoticeBean.redId = infoBean.getRedId();
                walletNoticeBean.refundTime = infoBean.getRefundTime();
                walletNoticeBean.source = infoBean.getSource();
                walletNoticeBean.refundType = infoBean.getRefundType();
                walletNoticeBean.title = infoBean.getTitle();
                walletNoticeBean.time = redpacketNoticeList.get(i).getTimeSend();
                list.add(walletNoticeBean);
            }
        }
        return list;
    }


    public static final class WalletNoticeBean implements Comparable<WalletNoticeBean> {
        public long time; //消息时间
        public String title;
        public String source;
        public String reason;
        public String redId;
        public long refundTime;//退款或者提现时间
        public float money;
        public String refundType;//退款方式

        @Override
        public int compareTo(WalletNoticeBean o) {
            return (int) (o.time - time);
        }
    }


}
